function [Result] = Ddavid_weighted_kNN_binary_classification(Weight, Label, K, KNNList)

% [Result] = Ddavid_weighted_kNN_classification(Weight, Label, K, KNNList)
%
% <Input>
% Weight: [n*1], n is the number of training instances, the weights of all
%         training instances
% Label: [n*1], the value is {-1, 0, 1}, the labels of all training
%        instances
% K: The K value of KNN
% KNNList: [m*k], The KNN List (for saving time), where m is the number of
%          testing instances
%
% <Output>
% Result: [m*1], double, the classification result which the range is
%         [-1.0, 1.0]

N = size(Label, 1);
M = size(KNNList, 1);
Result = zeros(M, 1);

TotalWeightTable = sum(abs(Weight(KNNList(:, 1:K))), 2);
WeightedkNNScoreTable = sum(abs(Weight(KNNList(:, 1:K))) .* Label(KNNList(:, 1:K)), 2);
for i = 1:M
    if(TotalWeightTable(i) ~= 0)
        Result(i) = WeightedkNNScoreTable(i) / TotalWeightTable(i);
    else
        Result(i) = 0;
    end
end
